Programmation PHP/Migration de Symfony 5 à 6

Un livre de Wikilivres.

La liste des dépréciations cassantes en Symfony 6 est disponible dans le Profiler d'un projet Symfony 5.4, à la section logs.

YAML[modifier | modifier le wikicode]

  • Dans security.firewalls.main, remplacer anonymous: true par lazy: true. Erreur sinon : Invalid firewall "main": user provider "users_in_memory" not found.
  • Dans framework.session, remplacer storage_id: session.storage.mock_file par storage_factory_id: session.storage.factory.mock_file. Erreur sinon : Unrecognized option "storage_id" under "framework.session".

PHP[modifier | modifier le wikicode]

  • Remplacer les injections de SessionInterface par RequestStack, et appeler sa méthode getSession(). Erreur sinon : "Cannot autowire service... SessionInterface".

Rector[modifier | modifier le wikicode]

Facultativement, on peut utiliser Rector pour convertir automatiquement les annotations en attributs[1].

Installation :

 composer require rector/rector --dev
 vendor/bin/rector init

Ensuite, il suffit de mettre les règles voulues dans rector.php :

    $rectorConfig->sets([
        LevelSetList::UP_TO_PHP_81,
        DoctrineSetList::ANNOTATIONS_TO_ATTRIBUTES,
        SymfonySetList::ANNOTATIONS_TO_ATTRIBUTES,
        SensiolabsSetList::FRAMEWORK_EXTRA_61,
    ]);

Si cela ne marche pas (depuis la v1), créer rector.php soit-même :

<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Doctrine\Set\DoctrineSetList;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Symfony\Set\SymfonySetList;

return RectorConfig::configure()
    ->withSymfonyContainerXml(__DIR__ . '/var/cache/dev/App_KernelDevDebugContainer.xml')
    ->withSets([
        LevelSetList::UP_TO_PHP_82,
        SymfonySetList::SYMFONY_CODE_QUALITY,
        SymfonySetList::SYMFONY_CONSTRUCTOR_INJECTION,
        DoctrineSetList::ANNOTATIONS_TO_ATTRIBUTES,
        SymfonySetList::ANNOTATIONS_TO_ATTRIBUTES,
]);

Puis lancer la modification des fichiers :

 vendor/bin/rector process src

Si les entités modifiées ne marchent plus (is not a valid entity or mapped super class), mettre à jour "doctrine/orm": "^2.9"[2] et dans doctrine.yaml, "type: annotation" par "type: attribute".

Twig[modifier | modifier le wikicode]

  • Remplacer controller(App\\Controller\\HomeController:headerAction) par (App\\Controller\\HomeController::headerAction). Sinon, prendre le nom du contrôleur avec sa méthode visible dans bin/console debug:router --show-controllers en échappant ses antislashs. Erreur sinon : The controller for URI "/_fragment" is not callable.

Références[modifier | modifier le wikicode]